package com.briup.ch10;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/13
 * @Description:
 */
/*
* 线程同步synchronized
* 同步代码块
* 同步方法
* 共享资源：个体，集体
* 个体：对象
* Object lock =new Object
* /this
* 集体：static
* 类名.class
* */
public class Test08 {
    public static int num=100;

    public static void main(String[] args) {
        Thread t1=new Thread(){
            @Override
            public void run() {
                while(num>0){
                    synchronized (Test08.class) {
                        num--;
                        System.out.println("t1剩余：" + num);
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t2=new Thread(){
            @Override
            public void run() {
                while(num>0){
                    synchronized (Test08.class) {
                        num--;
                        System.out.println("t2剩余：" + num);
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t3=new Thread(){
            @Override
            public void run() {
                while(num>0){
                    synchronized (Test08.class) {
                        num--;
                        System.out.println("t3剩余：" + num);
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t1.start();
        t2.start();
        t3.start();

    }
}
